Swoole 2.0正式版发布了。2.0版本最大的更新是增加了对协程(Coroutine)的支持。正式版已同时支持PHP5和PHP7。基于Swoole2.0协程PHP开发者可以已同步的方式编写代码,底层自动进行协程调度,转变为异步IO。解决了传统异步编程嵌套回调的问题。
与Node.js(ES6+)、Python等语言使用yield/generator、async/await的实现方式相比,Swoole协程无需修改代码添加额外的关键词。
与Go语言的goroutine相比,Swoole协程是内置式的,应用层代码无需添加go关键词启动协程,只需要使用封装好的协程客户端即可,使用更简单。另外Swoole协程的IO组件在底层内置了超时机制,不需要使用复杂的select/chan/timer实现客户端超时。
目前Swoole底层内置的协程客户端组件包括:udpclient、tcpclient、httpclient、redisclient、mysqlclient,基本涵盖了开发者常用的几种通信协议。协程组件只能在服务器的onConnect、onRequest、onReceive、onMessage 回调函数中使用。
$server = new Swoole\Http\Server('127.0.0.1', 9501);
/*
触发on request事件时,SWOOLE会开辟一个协程栈,对协程栈进行初始化
*/
$server->on('Request', function ($request, $response) {
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
/**
client在调用connect函数后,SWOOLE会将PHP上下文信息保存到当前栈内
然后将协程挂起,待确认连接成功后,触发epoll事件,然后协程切换
恢复PHP上下文信息,返回结果,继续执行PHP代码
*/
if ($tcp_cli->connect('127.0.0.1', 9906) === false) {
$response->end("connect server failed.");
return;
}
$tcp_cli->send('test for the coro');
/*
client在调用recv函数后,SWOOLE会将PHP上下文信息保存到当前栈内
然后将协程挂起待后端svr回包,触发epoll事件,然后协程切换
恢复PHP上下文信息,返回结果,继续执行PHP代码
如果后端在设定的超时时间内,未能回包,返回false
client的errCode定为110
*/
$ret = $tcp_cli->recv(100);
$tcp_cli->close();
if ($ret) {
$response->end(" swoole response is ok");
} else {
$response->end(" recv failed error : {$tcp_cli->errCode}");
}
});
$server->start();
$udp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
$ret = $udp_cli->connect('127.0.0.1', 9906);
$udp_cli->send('test for the coro');
$ret = $udp_cli->recv(100);
$udp_cli->close();
if ($ret)
{
$response->end("swoole response is ok");
}
else
{
$response->end("recv failed error : {$client->errCode}");
}
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
'host' => '127.0.0.1',
'user' => 'user',
'password' => 'pass',
'database' => 'test'
]);
$res = $swoole_mysql->query('select sleep(1)');
Redis客户端
$redis = new Swoole\Coroutine\Redis();
$redis->connect('127.0.0.1', 6379);
$val = $redis->get('key');
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->setHeaders([
'Host' => "localhost",
"User-Agent" => 'Chrome/49.0.2587.3',
'Accept' => 'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding' => 'gzip',
]);
$cli->set([ 'timeout' => 1]);
$cli->get('/index.php');
echo $cli->body;
$cli->close();
- Github:https://github.com/swoole/swoole-src/releases/tag/v2.0.5
- 开源中国:http://git.oschina.net/matyhtf/swoole/tree/v2.0.5
- PECL:https://pecl.php.net/package/swoole/2.0.5